package com.xinchen.gufeiwulian.ui.fragment

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.maple.base.base.BaseFragment
import com.maple.model.data.MainMenuData
import com.xinchen.gufeiwulian.R
import com.xinchen.gufeiwulian.ui.activity.MenuMoreActivity
import com.xinchen.gufeiwulian.ui.adapter.MainMenuAdapter
import com.xinchen.gufeiwulian.vm.HomeViewModel

class MainFragment(val viewModel: HomeViewModel) : BaseFragment() {

    private var rvList: RecyclerView? = null
    private val menus: MutableList<MainMenuData> = mutableListOf()
    private val list: MutableList<MainMenuData> = mutableListOf()


    private val listAdapter: MainMenuAdapter by lazy {
        MainMenuAdapter(requireContext()).apply {
            this.setListener(object : MainMenuAdapter.OnClickListener{
                override fun onItemClick(pos: Int, item: MainMenuData?) {
                    item?.let {
                        when(it.name) {
                            "入库" -> {

                            }
                            "出库" -> {

                            }
                            "更多" -> {
                                onStartActivity(MenuMoreActivity::class.java)
                            }
                        }
                    }
                }
            })
        }
    }

    companion object {
        @JvmStatic
        fun getInstance(viewModel: HomeViewModel): MainFragment {
            return MainFragment(viewModel)
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_main

    override fun initView(view: View, savedInstanceState: Bundle?) {
        super.initView(view, savedInstanceState)
        view.findViewById<TextView>(com.maple.res.R.id.tv_title_center)?.text = "首页"
        rvList = view.findViewById(R.id.rv_list)
        rvList?.let {
            val manager: GridLayoutManager = GridLayoutManager(requireContext(),4)
            manager.spanSizeLookup = object :GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    val itemType = it.adapter?.getItemViewType(position)
                    if(itemType == MainMenuAdapter.TYPE_HEADER) {
                        return 4
                    }
                    return 1
                }
            }

            it.layoutManager = manager
            it.adapter = listAdapter
        }
    }

    override fun initData(savedInstanceState: Bundle?) {

        menus.add(MainMenuData("入库", R.drawable.ic_menu_ruku))
        menus.add(MainMenuData("出库", R.drawable.ic_menu_chuku))
        menus.add(MainMenuData("标签查询", R.drawable.ic_menu_biaoqianchaxun))
        menus.add(MainMenuData("标签管理", R.drawable.ic_menu_biaoqianguanli))
        menus.add(MainMenuData("收集", R.drawable.ic_menu_shouji))
        menus.add(MainMenuData("装箱", R.drawable.ic_menu_zhuangxiang))
        menus.add(MainMenuData("跨省出库", R.drawable.ic_menu_kuashengchuku))
        menus.add(MainMenuData("待拉运", R.drawable.ic_menu_dailayun))

        if(menus.size > 7) {
            list.clear()
            list.addAll(menus.subList(0,7))
            list.add(MainMenuData("更多",R.drawable.ic_menu_gengduo))
        }
        listAdapter.setData(list)
    }


}